import os

from BaseThread import BaseThread


def cal_cpu_usage(data):
    items = data.split()
    idle = items[4].split('%')[0]
    usage = (800 - int(idle)) * 100 / 800
    return int(usage)


def get_cpu_usage():
    result = os.popen('adb -s 61A4B000188 shell top -n1|grep idle')
    result = result.readlines()[0]
    usage = cal_cpu_usage(result)
    print(usage)
    return usage


class CpuUsageThread(BaseThread):
    def __init__(self):
        super(CpuUsageThread, self).__init__("CpuUsage")

    def runnable(self):
        while self.alive:
            result = os.popen('adb shell top -n1|grep idle')
            result = result.readlines()[0]
            usage = cal_cpu_usage(result)
            print(usage)


if __name__ == "__main__":
    print(get_cpu_usage())
